//
//  UserData.swift
//  studay
//
//  Created by APPLE on 2024/12/16.
//


import Foundation

var encoder = JSONEncoder()
var decoder = JSONDecoder()

let TODO_KEY = "todoList2"

class ToDo :ObservableObject{
    @Published var toDoList:[SingleToDo]
    var userNotification:UserNotification = UserNotification()
    var count = 0
    init(){
        self.toDoList = []
    }
    init(toDoList: [SingleToDo]) {
        self.toDoList = []
        for item in toDoList {
            self.toDoList.append(SingleToDo(title: item.title,duedate: item.duedate,id:self.count,isFavorite:item.isFavorite))
            count+=1
            
        }
        
    }
    
    func check(id:Int){
        self.toDoList[id].isCheck.toggle()
        
    }
    
    
    func multiple(id:Int){
        self.toDoList[id].selected.toggle()
        
    }
    
    func add(title:String,duedate:Date,isFavorite:Bool){
        self.toDoList.append(SingleToDo(title:title,duedate:duedate,id:self.count,isFavorite: isFavorite))
        self.count += 1
        self.sort()
        self.dataStore()
        userNotification.addNotification(identifier: "\(title)\(duedate.timeIntervalSince1970)", content: UserNotification.createNotificationContent(title: title, subtitle: title, body: title, badge: 1), timeInterval: duedate.timeIntervalSinceNow)
    }
    
    func edit(id:Int,singleToDo:SingleToDo){
        self.toDoList[id].title = singleToDo.title
        self.toDoList[id].duedate = singleToDo.duedate
        self.toDoList[id].isCheck = false
        self.toDoList[id].isFavorite = singleToDo.isFavorite
        self.sort()
        self.dataStore()
        userNotification.addNotification(
            identifier: "\(singleToDo.title)\(singleToDo.duedate.timeIntervalSince1970)",
            content: UserNotification.createNotificationContent(title: singleToDo.title, subtitle: singleToDo.title, body: singleToDo.title, badge: 1),
            timeInterval: singleToDo.duedate.timeIntervalSinceNow
        )
        
    }
    
    func sort(){
        self.toDoList.sort { (data1,data2) in
            return data1.duedate.timeIntervalSince1970 > data2.duedate.timeIntervalSince1970
        }
        for i in 0..<self.toDoList.count {
            self.toDoList[i].id = i
        }
    }
    
    func del(id:Int){
        self.toDoList[id].deleted = true
        self.dataStore()
    }
    
    func dataStore(){
        UserDefaults.standard.set(try!encoder.encode(self.toDoList), forKey: TODO_KEY)
    }
    
    func delMultiple(){
        for i in 0..<toDoList.count {
            if(toDoList[i].selected && !toDoList[i].deleted){
                self.del(id:i)
            }
        }
    }
    
    func clearMultipe(){
        for i in 0..<toDoList.count {
            if(toDoList[i].selected && !toDoList[i].deleted){
                toDoList[i].selected = false
            }
        }
    }
    
    func favorite(id:Int){
        toDoList[id].isFavorite.toggle()
    }
    
    
     
}


struct SingleToDo:Identifiable,Codable {
    var title = "";
    var duedate:Date = Date();
    var isCheck:Bool = false;
    var id = 0
    var deleted:Bool = false;
    var selected:Bool = false;
    var isFavorite:Bool = false;
}


 
